package com.jz.digital.view;


import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

import com.jz.digital.R;
import com.jz.digital.utils.DipPixelUtil;

/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2019-12-19 10:54
 * @description 封装选项控件
 */
public class OptionView extends FrameLayout {

    private ImageView mOptionStartImg;
    private TextView mOptionStartText;
    private TextView mOptionEndText;
    private ImageView mOptionEndImg;
    private ImageView mOptionEndArrowImg;
    private Space mStartTextSpace;
    private Space mEndTextSpace;
    private View mDividerLine;

    private int mTextSize;
    private int mTextEndSize;
    private int mStartResourceId;
    private int mEndResourceId;
    private int mArrowResourceId;

    private String mStartText;
    private String mEndText;

    private boolean mStartImageVisible;

    private boolean mEndTextVisible;
    private boolean mEndImgVisible;

    private boolean mArrowVisible;
    private boolean mDeviderLineVisible;
    private boolean isBoldForStarText;

    private int mStartTextColor;
    private int mEndTextColor;

    private int colorList;

    private int mStartTextPadding;
    private int mEndTextPadding;
    private int startTextPadding;
    private int endTextPadding;
    /**
     * 副标题
     */
    private TextView mTvSubtitle;

    public OptionView(Context context) {
        super(context);
        initView(context);
    }

    public OptionView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initAttribute(context, attrs);

        initView(context);
    }

    /**
     * 初始化控件
     *
     * @param context
     */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_option, this, true);

        mOptionStartImg = findViewById(R.id.option_start_img);
        mOptionStartText = findViewById(R.id.option_start_text);

        mOptionEndText = findViewById(R.id.option_end_text);
        mOptionEndImg = findViewById(R.id.option_end_img);

        mOptionEndArrowImg = findViewById(R.id.option_end_arrow_img);

        mStartTextSpace = findViewById(R.id.start_text_space);
        mEndTextSpace = findViewById(R.id.end_text_space);
        mDividerLine = findViewById(R.id.option_divider_line);
        mTvSubtitle = findViewById(R.id.tv_subtitle);

        setTextSize(mTextSize);
        setTextEndSize(mTextEndSize);

        setStartImageResource(mStartResourceId);
        setStartText(mStartText == null ? "" : mStartText);

        setEndText(mEndText == null ? "" : mEndText);
        setEndImageResource(mEndResourceId);

        setStartImageVisible(mStartImageVisible);
        setEndTextVisible(mEndTextVisible);
        setEndImgVisible(mEndImgVisible);
        setArrowVisible(mArrowVisible);
        setDeviderLineVisible(mDeviderLineVisible);

        setStartTextColor(mStartTextColor);
        setEndTextColor(mEndTextColor);

        setStartTextPadding(mStartTextPadding);
        setEndTextPadding(mEndTextPadding);

        setBoldForStarText(isBoldForStarText);
        setArrowImageResource(mArrowResourceId);
    }

    public void setArrowImageResource(int mArrowResourceId) {
        mOptionEndArrowImg.setImageResource(mArrowResourceId);
    }

    private void setBoldForStarText(boolean isBoldForStarText) {
        mOptionStartText.getPaint().setFakeBoldText(isBoldForStarText);
    }

    /**
     * 初始化属性
     *
     * @param attrs
     */
    private void initAttribute(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OptionView);

        mTextSize = (int) a.getDimension(R.styleable.OptionView_optionTextSize, 12);
        mTextEndSize = (int) a.getDimension(R.styleable.OptionView_optionEndTextSize, 12);
        mStartText = a.getString(R.styleable.OptionView_optionStartText);
        mStartResourceId = a.getResourceId(R.styleable.OptionView_optionStartSrc, 0);
        mStartImageVisible = a.getBoolean(R.styleable.OptionView_optionStartImageVisible, true);
        mEndText = a.getString(R.styleable.OptionView_optionEndText);
        mEndResourceId = a.getResourceId(R.styleable.OptionView_optionEndSrc, 0);
        mEndTextVisible = a.getBoolean(R.styleable.OptionView_optionEndTextVisible, false);
        mEndImgVisible = a.getBoolean(R.styleable.OptionView_optionEndImgVisible, false);
        mArrowVisible = a.getBoolean(R.styleable.OptionView_optionArrowVisible, true);
        mDeviderLineVisible = a.getBoolean(R.styleable.OptionView_optionDeviderLineVisible, false);
        mStartTextColor = a.getColor(R.styleable.OptionView_optionStartTextColor, 0);
        mEndTextColor = a.getColor(R.styleable.OptionView_optionEndTextColor, 0);
        mStartTextPadding = a.getDimensionPixelSize(R.styleable.OptionView_optionStartTextPadding, 8);
        mEndTextPadding = a.getDimensionPixelSize(R.styleable.OptionView_optionEndTextPadding, 8);
        isBoldForStarText = a.getBoolean(R.styleable.OptionView_optionStartTextBold, false);
        mArrowResourceId = a.getResourceId(R.styleable.OptionView_optionArrowSrc, 0);
        a.recycle();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);


        /*if(!enabled) {
            setStartTextColor(Color.DKGRAY);
            setEndTextColor(Color.DKGRAY);
            setArrowVisible(false);

        }
        else{
            setStartTextColor(mStartTextColor);
            setEndTextColor(mEndTextColor);
            setArrowVisible(true);
        }*/
    }


    /**
     * 设置字体大小 单位为px
     *
     * @param size
     */
    public void setTextSize(float size) {
        mOptionStartText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        // mOptionEndText.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
    }

    /**
     * 设置字体大小 单位为px
     *
     * @param size
     */
    public void setTextEndSize(float size) {
        //mOptionStartText.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
        mOptionEndText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    /**
     * 设置起始位置图片
     *
     * @param drawable
     */
    public void setStartImageDrawable(Drawable drawable) {
        mOptionStartImg.setImageDrawable(drawable);
    }

    public String getStartText() {
        return mStartText;
    }

    /**
     * 设置起始位置图片
     *
     * @param resId
     */
    public void setStartImageResource(@DrawableRes int resId) {
        mOptionStartImg.setImageResource(resId);
    }

    /**
     * 设置起始位置文字
     *
     * @param text
     */
    public void setStartText(String text) {
        mOptionStartText.setText(text);
    }


    /**
     * 设置起始位置文字
     *
     * @param resId
     */
    public void setStartText(@StringRes int resId) {
        mOptionStartText.setText(resId);
    }

    /**
     * 设置末尾位置图片
     *
     * @param drawable
     */
    public void setEndImageDrawable(Drawable drawable) {
        mOptionEndImg.setImageDrawable(drawable);
    }

    /**
     * 设置末尾位置图片
     *
     * @param resId
     */
    public void setEndImageResource(@DrawableRes int resId) {
        mOptionEndImg.setImageResource(resId);
    }

    /**
     * 设置起末尾位置文字
     *
     * @param text
     */
    public void setEndText(String text) {
        mOptionEndText.setText(text);
    }

    public TextView getOptionEndText() {
        return mOptionEndText;
    }

    /**
     * 得到末尾位置的文字
     *
     * @return
     */
    public String getEndText() {
        return mOptionEndText.getText().toString();
    }

    /**
     * 设置末尾位文字
     *
     * @param resId
     */
    public void setEndText(@StringRes int resId) {
        mOptionEndText.setText(resId);
    }

    /**
     * 设置箭头是否可见
     *
     * @param visible true 可见,false 隐藏
     */
    public void setArrowVisible(boolean visible) {
        int visibility = visible ? View.VISIBLE : View.GONE;
        mOptionEndArrowImg.setVisibility(visibility);
    }

    /**
     * 设置下划线是否可见
     *
     * @param visible true 可见,false 隐藏
     */
    public void setDeviderLineVisible(boolean visible) {
        int visibility = visible ? View.VISIBLE : View.GONE;
        mDividerLine.setVisibility(visibility);
    }

    /**
     * 设置末尾文字显示状态
     *
     * @param visible true 显示,false 隐藏
     */
    public void setEndTextVisible(boolean visible) {
        int visibility = visible ? View.VISIBLE : View.GONE;
        mOptionEndText.setVisibility(visibility);
    }

    /**
     * 设置末尾图片显示状态
     *
     * @param visible true 显示,false 隐藏
     */
    public void setEndImgVisible(boolean visible) {
        int visibility = visible ? View.VISIBLE : View.INVISIBLE;
        mOptionEndImg.setVisibility(visibility);
    }

    /**
     * 设置起始位置图片显示装态
     *
     * @param visible true 显示,false 隐藏
     */
    public void setStartImageVisible(boolean visible) {
        int visibility = visible ? View.VISIBLE : View.GONE;
        mOptionStartImg.setVisibility(visibility);
    }

    /**
     * 设置左侧图片颜色
     *
     * @param color
     */
    public void setStartImageTint(int color) {
        mOptionStartImg.setImageTintList(ColorStateList.valueOf(color));
    }

    public void setStartTextColor(int color) {
        this.mOptionStartText.setTextColor(color);
    }

    public void setEndTextColor(int color) {
        this.mOptionEndText.setTextColor(color);
    }


    public void setStartTextPadding(int padding) {
        // -1 == MATCH_PARENT
        mStartTextSpace.setLayoutParams(new LinearLayout.LayoutParams(DipPixelUtil.dip2px(padding),
                -1));
    }

    protected void setEndTextPadding(int padding) {
        // -1 == MATCH_PARENT
        mEndTextSpace.setLayoutParams(new LinearLayout.LayoutParams(padding, -1));
    }

    /**
     * 设置副标题
     */
    public void setSubtitle(String text) {
        mTvSubtitle.setText(text);
        mTvSubtitle.setVisibility(View.VISIBLE);
    }
}
